package com.example.music.ui.recommend;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.example.music.MainActivity;
import com.example.music.R;
import com.example.music.adapter.UpListAdapter;
import com.example.music.databinding.FragmentRecommendBinding;
import com.example.music.entity.DataMusic;
import com.example.music.dialog.CustomDialog;
import com.hacknife.carouselbanner.interfaces.OnCarouselItemClickListener;

import java.util.List;

public class RecommendFragment extends Fragment {

    private RecommendViewModel homeViewModel;
    private FragmentRecommendBinding binding;
    private List<DataMusic> musics;
    private UpListAdapter upListAdapter;
    private UpListAdapter newListAdapter;
    private UpListAdapter heartListAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel = new ViewModelProvider(this).get(RecommendViewModel.class);
        binding = FragmentRecommendBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        binding.banner.setOnCarouselItemClickListener(new OnCarouselItemClickListener() {
                    @Override
                    public void onItemClick(int position, String url) {
                        if(musics ==null||musics.size()<=position-1) {
                            return;
                        }
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("musics",musics.get(position));
                        Navigation.findNavController(binding.getRoot()).navigate(R.id.action_navigation_recommend_to_navigation_play,bundle);
                    }
                });
        binding.banner.initBanner(homeViewModel.getList());
        binding.personal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.action_navigation_recommend_to_navigation_personal);
            }
        });
        homeViewModel.getDataBanner().observe(getViewLifecycleOwner(), new Observer<List<DataMusic>>() {
            @Override
            public void onChanged(List<DataMusic> dataMusics) {
                musics=dataMusics;
            }
        });
        homeViewModel.getDataUP().observe(getViewLifecycleOwner(), new Observer<List<DataMusic>>() {
            @Override
            public void onChanged(List<DataMusic> dataMusics) {
                upListAdapter = new UpListAdapter(dataMusics);
                binding.upList.setAdapter(upListAdapter);
                binding.upList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("musics",dataMusics.get((int) id));
                        Navigation.findNavController(view).navigate(R.id.action_navigation_recommend_to_navigation_play,bundle);
                    }
                });
            }
        });
        homeViewModel.getDataNew().observe(getViewLifecycleOwner(), new Observer<List<DataMusic>>() {
            @Override
            public void onChanged(List<DataMusic> dataMusics) {
                newListAdapter = new UpListAdapter(dataMusics);
                binding.newList.setAdapter(newListAdapter);
                binding.newList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("musics",dataMusics.get((int) id));
                        Navigation.findNavController(view).navigate(R.id.action_navigation_recommend_to_navigation_play,bundle);
                    }
                });
            }
        });
        homeViewModel.getDataHeart().observe(getViewLifecycleOwner(), new Observer<List<DataMusic>>() {
            @Override
            public void onChanged(List<DataMusic> dataMusics) {
                heartListAdapter = new UpListAdapter(dataMusics);
                binding.heatList.setAdapter(heartListAdapter);
                binding.heatList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("musics",dataMusics.get((int) id));
                        Navigation.findNavController(view).navigate(R.id.action_navigation_recommend_to_navigation_play,bundle);
                    }
                });
            }
        });
        homeViewModel.getErr().observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                CustomDialog.Builder builder = new CustomDialog.Builder(getContext());
                builder.setTitle("访问异常");
                builder.setMessage("数据获取失败，请检查网络状态");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });
        ((MainActivity) requireActivity()).visibleAll();
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}